package byggeTegning.geometry;

//import DEBUGGING.*;  // Search for DEBUG.<INFO|TRACE| ... >

import javax.media.j3d.*;
import javax.vecmath.*;

public class TransformedGroup3D extends TransformGroup
{
  static final Vector3d xAxis=new Vector3d(1.0,0.0,0.0);
  static final Vector3d yAxis=new Vector3d(0.0,1.0,0.0);
  static final Vector3d zAxis=new Vector3d(0.0,0.0,1.0);
  
  double angleX=0.0f;
  double angleY=0.0f;
  double angleZ=0.0f;
  double scale=1.0f;
  double xScale=1.0f;
  double yScale=1.0f;
  double zScale=1.0f;
  Vector3d p0=new Vector3d(0.0,0.0,0.0);

  public TransformedGroup3D()
  {
  }
  
  public void setAngleX(double angleX) {this.angleX =angleX; updateTransform();}  
  public void incAngleX(double angleX) {this.angleX+=angleX; updateTransform();}  
  public void setAngleY(double angleY) {this.angleY =angleY; updateTransform();}  
  public void incAngleY(double angleY) {this.angleY+=angleY; updateTransform();}  
  public void setAngleZ(double angleZ) {this.angleZ =angleZ; updateTransform();}  
  public void incAngleZ(double angleZ) {this.angleZ+=angleZ; updateTransform();}  

  public void setSize(double width,double height,double depth)
  { this.xScale=width;
    this.yScale=height;
    this.zScale=depth;
    updateTransform();
  }  

  public void setScale(double scale)
  { this.scale=scale;
    updateTransform();
  }  
    
  public void setP0(double x,double y,double z)
  { this.p0.x=x;
    this.p0.y=y;
    this.p0.z=z;
    updateTransform();
  }
  
  public void setAngles(Vector3d direction)
  { this.angleX=direction.angle(xAxis);
    this.angleY=direction.angle(yAxis);
    this.angleZ=direction.angle(zAxis);
    updateTransform();
  }  
  
  public void incAngles(Vector3d direction)
  { this.angleX+=direction.angle(xAxis);
    this.angleY+=direction.angle(yAxis);
    this.angleZ+=direction.angle(zAxis);
    updateTransform();
  }  
  
  private void updateTransform()
  {
    Transform3D trans = new Transform3D();
//    this.getTransform(trans);

    if(angleX!=0.0)
    { Transform3D t=new Transform3D(); t.rotX(angleX); trans.mul(t); }

    if(angleY!=0.0)
    { Transform3D t=new Transform3D(); t.rotY(angleY); trans.mul(t); }

    if(angleZ!=0.0)
    { Transform3D t=new Transform3D(); t.rotZ(angleZ); trans.mul(t); }
    
    trans.setScale(new Vector3d(xScale*scale,yScale*scale,zScale*scale));
//    trans.setScale(scale);

    trans.setTranslation(p0);
//    DEBUG.INFO("*** Transform used ***\n"+trans);
    this.setTransform(trans);
  }  
}
